package com.mujin.keji.collectionsystem.module.outboundtask.repository;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.os.Environment;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.mujin.keji.collectionsystem.Constant;
import com.mujin.keji.collectionsystem.base.BaseRepository;
import com.mujin.keji.collectionsystem.module.updata.dto.VisitUserDto;
import com.mujin.keji.collectionsystem.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 录音，照片文件获取
 *
 * @author weishixiong
 * @Time 2018-03-28
 */

public class BoundDetailBaseRepository extends BaseRepository implements IBoundDetailBaseRepository {
    private String TAG = "PhotoFilePresenter";

    @Override
    public LiveData<List<File>> getPhotoFilesCount() {
        List<File> fileList = new ArrayList<>();
        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/temp" );
            File[] files = file.listFiles();
            for (int j = 0; j < files.length; j++) {
                String name = files[j].getName();
                if (files[j].isDirectory()) {
                    String dirPath = files[j].toString().toLowerCase();
                } else if (files[j].isFile() & name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".bmp") || name.endsWith(".gif") || name.endsWith(".jpeg") || name.endsWith(".txt")) {
                    fileList.add(files[j]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MutableLiveData<List<File>> data = new MutableLiveData<>();
            data.setValue(fileList);
            return data;
        }

    }

    @Override
    public LiveData<List<File>> getRecordFilesCount() {
        List<File> fileList = new ArrayList<>();
        try {
            File file = new File(Constant.Param.RECORDPATH);
            File[] files = file.listFiles();
            for (int j = 0; j < files.length; j++) {
                if (!files[j].isDirectory()) {
                    // String dirPath = files[j].toString().toLowerCase();
                    fileList.add(files[j]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MutableLiveData<List<File>> data = new MutableLiveData<>();
            data.setValue(fileList);
            return data;
        }
    }

    @Override
    public LiveData<List<VisitUserDto>> getPhotoFilesCountById(String applyId, String userCount) {
        List<VisitUserDto> visitUserDtos = new ArrayList<>();
        try {

            File file = new File(Environment.getExternalStorageDirectory() + "/temp" );

            File[] files = file.listFiles();
            for (int j = 0; j < files.length; j++) {
                String name = files[j].getName();
                if (files[j].isDirectory()) {
                    String dirPath = files[j].toString().toLowerCase();
                } else if (files[j].isFile() & name.endsWith(".txt")) {
                    String visitStr = FileUtil.readJsonFromFile(files[j]);
                    VisitUserDto visitUserDto = JSONObject.parseObject(visitStr, VisitUserDto.class);
                    if (applyId.equals(visitUserDto.getVisitApplyId()) && userCount.equals(visitUserDto.getUserAccount())) {
                        if(new File(visitUserDto.getFilePath()).exists()){
                            visitUserDtos.add(visitUserDto);
                        }
                        else{
                            FileUtil.deleteFile(files[j]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MutableLiveData<List<VisitUserDto>> data = new MutableLiveData<>();
            data.setValue(visitUserDtos);
            return data;
        }
    }

    @Override
    public LiveData<List<VisitUserDto>> getRecordFilesCountById(String applyId, String userCount) {
        List<VisitUserDto> visitUserDtos = new ArrayList<>();
        try {
            File file = new File(Constant.Param.RECORDPATH);
            File[] files = file.listFiles();
            for (int j = 0; j < files.length; j++) {
                String name = files[j].getName();
                if (files[j].isFile() & name.endsWith(".txt")) {
                    String visitStr = FileUtil.readJsonFromFile(files[j]);
                    VisitUserDto visitUserDto = JSONObject.parseObject(visitStr, VisitUserDto.class);
                    if (applyId.equals(visitUserDto.getVisitApplyId()) && userCount.equals(visitUserDto.getUserAccount())) {
                        if(new File(visitUserDto.getFilePath()).exists()){
                            visitUserDtos.add(visitUserDto);
                        }
                        else{
                            FileUtil.deleteFile(files[j]);
                        }


                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MutableLiveData<List<VisitUserDto>> data = new MutableLiveData<>();
            data.setValue(visitUserDtos);
            return data;
        }
    }
}
